<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2019/7/2
 * Time: 23:04
 */

namespace app\home\model;

/**
 * 请假
 * Class ApprovalLeave
 * @package app\home\model
 */
class ApprovalLeave extends Base
{
    protected $insert = [
        'apply_time' => NOW_TIME,
        'status' => 0
    ];

    /**
     * 获取已使用请假天数
     * @param $userId
     * @param int $leaveId
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getUsersUsedDays($userId,$leaveId = 0)
    {
        $startTime = strtotime(date("Y-01-01 00:00:00"));
        $endTime = strtotime(date("Y-12-31 23:59:59"));

         $builder = self::where('status','in',[0,1,3])
            ->where('apply_user',$userId)
            ->where('apply_time','between',[$startTime,$endTime]);
         if(!empty($leaveId)) {
             $builder->where('type',$leaveId);
         }
         return $builder->group('type')
            ->field('sum(duration) as true_used_days,type')
            ->select();
    }
}